//
//  CXSearchBar.m
//  Meeting_iPad
//
//  Created by xiuyong.huang on 12-3-23.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "CXSearchBar.h"

@interface CXSearchBar (Private)

-(void)setDefault;

@end

NSString  *const CXSearchBarResignFirstResponderNotication=@"CXSearchBarResignFirstResponderNotication";

@implementation CXSearchBar

@synthesize backgroundView=_backgroundView;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setDefault];
       
    }
    return self;
}

-(id)initWithCoder:(NSCoder *)aDecoder{

    self=[super initWithCoder:aDecoder];
    if (self) {
        
        [self setDefault];
    }
    
    return self;
}

-(void)setDefault{

    for (UIView * view in self.subviews) {
        if ([view isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
            [view removeFromSuperview];
            break;
        }
    }
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(resignFirstResponder) name:CXSearchBarResignFirstResponderNotication object:nil];
}

-(void)dealloc{

    [_backgroundView release];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:CXSearchBarResignFirstResponderNotication object:nil];
    [super dealloc];
}

-(void)setBackgroundView:(UIView *)backgroundView{

    if ([_backgroundView isEqual:backgroundView]) {
        return;
    }
    [_backgroundView removeFromSuperview];
    [_backgroundView release];
    _backgroundView=[backgroundView retain];
    
    _backgroundView.frame=CGRectMake(0.0f, 0.0f, self.frame.size.width, self.frame.size.height);
    _backgroundView.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    [self insertSubview:_backgroundView atIndex:0];
}


@end
